<?php

/**
 * Radcodes - SocialEngine Module
 *
 * @category   Application_Extensions
 * @package    Badge
 * @copyright  Copyright (c) 2009-2010 Radcodes LLC (http://www.radcodes.com)
 * @license    http://www.radcodes.com/license/
 * @version    $Id$
 * @author     Vincent Van <vincent@radcodes.com>
 */

class Badge_AdminRequestsController extends Core_Controller_Action_Admin {
	
	public function init() {
		
		if (! Engine_Api::_ ()->core ()->hasSubject ()) {
			if (0 !== ($request_id = ( int ) $this->_getParam ( 'request_id' )) && null !== ($request = Engine_Api::_ ()->getItem ( 'badge_request', $request_id ))) {
				Engine_Api::_ ()->core ()->setSubject ( $request );
			}
		}
		
		$this->_helper->requireSubject->setActionRequireTypes ( array ('delete' => 'badge_request', 'edit' => 'badge_request' ) );
	
	}
	
	public function indexAction() {
		$this->view->navigation = $navigation = Engine_Api::_ ()->getApi ( 'menus', 'core' )->getNavigation ( 'badge_admin_main', array (), 'badge_admin_main_requests' );
		
		$this->view->formFilter = $formFilter = new Badge_Form_Admin_Request_Filter ();
		
		// Process form
		$values = array ();
		if ($formFilter->isValid ( $this->_getAllParams () )) {
			$values = $formFilter->getValues ();
		}
		$values = Engine_Api::_ ()->badge ()->filterEmptyParams ( $values );
		
		$this->view->formValues = $values;
		
		$this->view->assign ( $values );
		
		$this->view->paginator = Engine_Api::_ ()->badge ()->getRequestsPaginator ( $values );
		$this->view->paginator->setItemCountPerPage ( 10 );
		$this->view->paginator->setCurrentPageNumber ( $this->_getParam ( 'page', 1 ) );
		$this->view->params = $values;
	
	}
	
	public function editAction() {
		
		$this->view->request = $request = Engine_Api::_ ()->core ()->getSubject ( 'badge_request' );
		
		$this->view->form = $form = new Badge_Form_Admin_Request_Edit ();
		$form->populate ( $request->toArray () );
		
		if (! $this->getRequest ()->isPost ()) {
			if (! $request->read) {
				$request->read = 1;
				$request->save ();
			}
			return;
		}
		
		if (! $form->isValid ( $this->getRequest ()->getPost () )) {
			return;
		}
		
		$table = Engine_Api::_ ()->getDbtable ( 'requests', 'badge' );
		
		$db = $table->getDefaultAdapter ();
		$db->beginTransaction ();
		
		try {
			$values = $form->getValues ();
			$request->setFromArray ( $values );
			$request->save ();
			
			$db->commit ();
		} 

		catch ( Exception $e ) {
			$db->rollBack ();
			throw $e;
		}
		
		// Close smoothbox
		$currentContext = $this->_helper->contextSwitch->getCurrentContext ();
		if (null === $currentContext) {
			return $this->_helper->redirector->gotoRoute ( array ('module' => 'badge', 'controller' => 'requests' ), 'admin_default', true );
		} else if ('smoothbox' === $currentContext) {
			if ($request->status == 'approved') {
				$params = array ('module' => 'badge', 'controller' => 'awards', 'action' => 'create', 'badge_id' => $request->badge_id, 'user_id' => $request->user_id );
				$redirectUrl = $this->getFrontController ()->getRouter ()->assemble ( $params, 'admin_default', true );
				return $this->_forward ( 'success', 'utility', 'core', array ('parentRedirectTime' => 10, 'parentRedirect' => $redirectUrl, 'messages' => array (Zend_Registry::get ( 'Zend_Translate' )->_ ( 'Redirecting to create form ...' ) ) ) );
			} else {
				return $this->_forward ( 'success', 'utility', 'core', array ('messages' => $this->view->translate ( 'Your changes have been saved.' ), 'smoothboxClose' => true, 'parentRefresh' => true ) );
			}
		
		}
	
	}
	
	public function deleteAction() {
		
		$this->view->request = $request = Engine_Api::_ ()->core ()->getSubject ( 'badge_request' );
		
		// Check post
		if ($this->getRequest ()->isPost ()) {
			$db = Engine_Db_Table::getDefaultAdapter ();
			$db->beginTransaction ();
			
			try {
				$request->delete ();
				$db->commit ();
				
				Engine_Api::_ ()->core ()->clearSubject ();
			} 

			catch ( Exception $e ) {
				$db->rollBack ();
				throw $e;
			}
			
			$this->_forward ( 'success', 'utility', 'core', array ('smoothboxClose' => 10, 'parentRefresh' => 10, 'messages' => Zend_Registry::get ( 'Zend_Translate' )->_ ( "Request deleted successfully." ) ) );
		}
	}

}